package cn.tedu.hongbeifang.controller;
import cn.tedu.hongbeifang.pojo.dto.BannerAddNewDTO;
import cn.tedu.hongbeifang.pojo.dto.BannerUpdateDTO;
import cn.tedu.hongbeifang.pojo.vo.BannerListItemVO;
import cn.tedu.hongbeifang.pojo.vo.BannerStandardVO;
import cn.tedu.hongbeifang.service.IBannerService;
import cn.tedu.hongbeifang.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 处理相册相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "02. 轮播图管理模块")
@Slf4j
@RestController
@RequestMapping("/banners")
@Validated
public class BannerController {

  @Autowired
  IBannerService bannerService;

    public BannerController() {
        log.debug("创建控制器类对象：BannerController");
    }
    // http://localhost:9081/banners/upload
    @RequestMapping("/upload")
    @ApiOperation("上传轮播图")
    @ApiOperationSupport(order = 10)
    public JsonResult<String> upload(MultipartFile picFile) throws IOException {
        log.debug("开始处理【上传轮播图】的请求，参数：{}", picFile);
        String filename = bannerService.upload(picFile);
        return JsonResult.ok(filename);
    }
    // http://localhost:9081/banners/remove
    @RequestMapping("/remove")
    @ApiOperation("删除轮播图")
    @ApiOperationSupport(order = 20)
    public JsonResult<Void> remove(String url) {
        log.debug("开始处理【删除轮播图】的请求，参数：{}", url);
        bannerService.remove(url);
        return JsonResult.ok();
    }
    // http://localhost:9081/banners/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加轮播图")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid BannerAddNewDTO bannerAddNewDTO) {
        log.debug("开始处理【添加轮播图】的请求，参数：{}", bannerAddNewDTO);
        bannerService.addNew(bannerAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9081/banners/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除轮播图")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID删除轮播图】的请求，参数：{}", id);
        bannerService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/banners/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改轮播图详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                     @Valid BannerUpdateDTO bannerUpdateDTO) {
        log.debug("开始处理【修改轮播图详情】的请求，ID：{}，新数据：{}", id, bannerUpdateDTO);
        bannerService.updateInfoById(id, bannerUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9081/banners/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询轮播图详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<BannerStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询轮播图详情】的请求，参数：{}", id);
        BannerStandardVO queryResult = bannerService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9081/banners
    @GetMapping("")
    @ApiOperation("查询轮播图列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<BannerListItemVO>> list() {
        log.debug("开始处理【查询轮播图列表】的请求，无参数");
        List<BannerListItemVO> list = bannerService.list();
        return JsonResult.ok(list);
    }



}
